package com.ruoyi.attendance.controller;

import com.ruoyi.attendance.utils.BaiduFaceMatchUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

/**
 * @Author: @Weiney
 * @ClassName: FaceRecognitionController
 * @Description: 人脸对比接口
 * @Create: 2025-03-08
 **/

@RestController
@RequestMapping("/face")
public class FaceRecognitionController {

    @Autowired
    private BaiduFaceMatchUtil baiduFaceMatchUtil;

    @PostMapping("/match")
    @PreAuthorize("@ss.hasPermi('attendance:sign:face')")
    public String faceMatch(@RequestParam("signPhoto") MultipartFile signPhoto,
                            @RequestParam("avatarPhoto") MultipartFile avatarPhoto) throws IOException {
        // 将实时上传的拍照照片转换为 Base64 编码
        String livePhoto = baiduFaceMatchUtil.imageToBase64(signPhoto.getBytes());
        // 将用户头像照片转换为 Base64 编码
        String originalPhoto = baiduFaceMatchUtil.imageToBase64(avatarPhoto.getBytes());

        // 调用百度人脸对比 API 进行对比
        return baiduFaceMatchUtil.faceMatch(originalPhoto, livePhoto);
    }
}